// subpackage/pages/userRegisterV2/stepThree.js
import requestUtil from '../../../baseRequest'
import urls from '../../../requestUrl'
import commentUtils from '../../../utils/util.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    registerInfo: {},
    showLoading:false,
    
  },
  countNextWeek() {
    const today = new Date();
    const nextWeekDay = new Date(today);
    nextWeekDay.setDate(today.getDate() + 7);
    this.setData({
      alarmStr: (nextWeekDay.getMonth() + 1) + "月" + nextWeekDay.getDate() + "日" + "后恢复参考价￥299"
    })
  },
  confirmPay() {
    //1.新建用户
    let submitUrl = urls.driver.registerWithCard;
    requestUtil.postRequest(submitUrl, this.data.registerInfo, (res) => {
      if (res.isFail) {
        wx.showModal({
          title: '操作失败',
          content: '提交资料失败：'+res.msg,
          showCancel: false,
          success: res => {
            this.setData({
              showLoading: false
            });
          }
        })
        return
      }
      //2.发起微信支付
      wx.requestPayment({
        timeStamp: res.timestamp,
        nonceStr: res.nonce_str,
        package: res.package,
        signType: 'MD5',
        paySign: res.pay_sign,
        success: (res) => {
          this.handelSuccessPayment()
        },
        fail: function () {
          wx.showModal({
            title: '温馨提醒',
            content: '支付未成功！请重新发起支付',
            showCancel: false,
            confirmText: '确认关闭'
          })
        },
      })
    });
  },
  handelSuccessPayment() {
    const callBackData = {
      openid:wx.getStorageSync('openid')
    }
    let callBackUrl = urls.driver.wxPayCallBack;
    requestUtil.postRequest(callBackUrl, callBackData, (res) => {
      getApp().updateGlobalUserRegisterStatus(true);
      getApp().updateGlobalUserDriverStatus(2);
      wx.showModal({
        title: '温馨提示',
        content: '注册成功',
        showCancel: false,
        success: res => {
          if (res.confirm) {
            wx.switchTab({
              url: '/pages/user/personal/index',
            })
            this.setData({
              showLoading: false
            });
          }
        }
      })
      })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let registerInfo = wx.getStorageSync('registerInfo');
    if (registerInfo) {
      this.setData({
        registerInfo
      })
    }
    this.countNextWeek();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})